package com.liguang.rcs.admin.web.drivermgr;

import com.liguang.rcs.admin.db.entity.DodmsDriverEntity;
import com.liguang.rcs.admin.util.copy.BeanUtils;
import com.liguang.rcs.admin.util.copy.CopyProperty;
import com.liguang.rcs.admin.util.copy.EnableCopyProperties;
import com.liguang.rcs.admin.util.copy.converter.StringToTimestampConverter;
import com.liguang.rcs.admin.util.excel.ExcelColumnProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.Calendar;

@Data
@EnableCopyProperties
@Slf4j
public class DriverVO {
    @CopyProperty
    private Long id;
    @CopyProperty
    @ExcelColumnProperty(columnIndex = 1)
    private String name; //司机名字
    @CopyProperty
    private String gender; //司机性别
    @CopyProperty(typeCovertClass = StringToTimestampConverter.class, extMsg = "yyyy/MM/dd")
    private String birthday;// 司机出生年月
    @CopyProperty
    private String nation; //司机名族
    @CopyProperty
    private String age; //司机年龄
    @CopyProperty
    private String address; //司机住址
    @CopyProperty
    private String tel; //手机号码
    @CopyProperty
    private String photoBase64; //照片

    /**
     *
     */
    @CopyProperty
    @ExcelColumnProperty(columnIndex = 2)
    private String company; //所属公司
    @CopyProperty
    @ExcelColumnProperty(columnIndex = 3)
    private String driverLicense;//驾驶证号
    @CopyProperty
    private String trafficPolice; //所属交通大队
    @CopyProperty
    private String certificateNo; //合格证号
    @CopyProperty(typeCovertClass = StringToTimestampConverter.class, extMsg = "yyyy/MM/dd")
    private String certificateDate; //理论考试时间
    @CopyProperty(typeCovertClass = StringToTimestampConverter.class, extMsg = "yyyy/MM/dd")
    @ExcelColumnProperty(columnIndex = 4)
    private String theoryExaminationTime; //理论考试时间
    @CopyProperty
    @ExcelColumnProperty(columnIndex = 5)
    private String theoryResult; //理论考试结果
    @CopyProperty(typeCovertClass = StringToTimestampConverter.class, extMsg = "yyyy/MM/dd")
    @ExcelColumnProperty(columnIndex = 6)
    private String resitTheoryExaminationTime;// 理论补考时间
    @CopyProperty
    @ExcelColumnProperty(columnIndex = 7)
    private String resitTheoryResult; //理论补考结果

    @CopyProperty(typeCovertClass = StringToTimestampConverter.class, extMsg = "yyyy/MM/dd")
    @ExcelColumnProperty(columnIndex = 8)
    private String skillExaminationTime; //技能考试时间
    @CopyProperty
    @ExcelColumnProperty(columnIndex = 9)
    private String skillResult; //技能考试结果
    @CopyProperty(typeCovertClass = StringToTimestampConverter.class, extMsg = "yyyy/MM/dd")
    @ExcelColumnProperty(columnIndex = 10)
    private String resitSkillExaminationTime; //技能补考时间
    @CopyProperty
    @ExcelColumnProperty(columnIndex = 11)
    private String resitSkillResult; //技能补考结果

    @CopyProperty
    @ExcelColumnProperty(columnIndex = 12)
    private String remark; //备注

    private String updateDate; //更新时间

    /**
     * 通过entity生成vo对象
     */
    public static DriverVO buildFrom(DodmsDriverEntity entity) {
        try {
            DriverVO vo = new DriverVO();
            BeanUtils.reverseCopyProperties(vo, entity);
            if (entity.getBirthday() != null) {
                Calendar instance = Calendar.getInstance();
                int year = instance.get(Calendar.YEAR);
                instance.setTimeInMillis(entity.getBirthday().getTime());
                vo.setAge(String.valueOf(year - instance.get(Calendar.YEAR)));
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            vo.setUpdateDate(sdf.format(entity.getUpdateDate()));
            return vo;
        } catch (Exception ex) {
            log.warn("[Account] build UserVO fail, Exception:{}", ex);
            return null;
        }
    }

    public DodmsDriverEntity toEntity() {
        try {
            DodmsDriverEntity entity = new DodmsDriverEntity();
            BeanUtils.copyProperties(this, entity);
            return entity;
        } catch (Exception ex) {
            log.warn("[Account] build DodmsUserEntity fail, Exception:{}", ex);
            return null;
        }
    }
}
